<?php

// 9/5/2009: immanuel192 rewrite this class

class imagegallerymodule {
	function name() { return 'Image Gallery'; }
	function description() { return 'Allows a user to post images to galleries.'; }
	function author() { return 'Immanuel192 - ABSoft.vn'; }
	
	function hasSources() { return true; }
	function hasContent() { return true; }
	function hasViews() { return true; }
	
	function supportsWorkflow() { return false; }
	
	function getLocationHierarchy($loc) {
		return array(exponent_core_makeLocation($loc->mod,$loc->src),$loc);
	}
	
	function permissions($internal = '') {
		if ($internal == '') {
			return array(
				'administrate'=>'Administrate',
				'configure'=>'Configure',
				'create'=>'Create Galleries',
				'edit'=>'Edit Galleries',
				'delete'=>'Delete Galleries',
				'manage'=>'Manage Gallery Images'
			);
		} else {
			return array(
				'administrate'=>'Administrate',
				'configure'=>'Configure',
				'edit'=>'Edit Gallery',
				'delete'=>'Delete Gallery',
				'manage'=>'Manage Gallery Images'
			);
		}
	}
	
	function show($view,$loc = null, $title = '') {
		$_REQUEST['page']=1;
		exponent_flow_set(SYS_FLOW_PUBLIC,SYS_FLOW_SECTIONAL);
		include("actions/view_page.php");
	}
	
	function deleteIn($loc) {
		global $db;
		foreach ($db->selectObjects('imagegallery_gallery',"location_data='".serialize($loc)."'") as $gallery) {
			$db->delete('imagegallery_image','gallery_id='.$gallery->id);
		}
		$db->delete('imagegallery_gallery',"location_data='".serialize($loc)."'");
	}
	
	function copyContent($oloc,$nloc) {
		global $db;
		$basedirectory = 'files/imagegallerymodule/'.$nloc->src;
		
		foreach ($db->selectObjects('imagegallery_gallery',"location_data='".serialize($oloc)."'") as $gallery) {
			$old_id = $gallery->id;
			unset($gallery->id);
			$gallery->location_data = serialize($nloc);
			$gallery->id = $db->insertObject($gallery,'imagegallery_gallery');
			
			$directory = $basedirectory . '/gallery'.$gallery->id;
			if (!defined('SYS_FILES')) require_once(BASE.'subsystems/files.php');
			if (!file_exists(BASE.$directory) && exponent_files_makeDirectory($directory) != SYS_FILES_SUCCESS) {
				return;
			}
			
			foreach ($db->selectObjects('imagegallery_image','gallery_id='.$old_id) as $image) {
				$file = $db->selectObject('file','id='.$image->file_id);
				copy(BASE.$file->directory.'/'.$file->filename,BASE.$directory.'/'.$file->filename);
				if (file_exists(BASE.$directory.'/'.$file->filename)) {
					$file->directory = $directory;
					unset($file->id);
					$image->file_id = $db->insertObject($file,'file');
					
					unset($image->id);
					$image->gallery_id = $gallery->id;
					$db->insertObject($image,'imagegallery_image');
				}
			}
		}
	}
	
	function searchName() {
	    $lang=exponent_lang_loadFile("modules/imagegallerymodule/class.php");
			return $lang['search_name'];
	}
	
	function spiderContent($item = null) {
		global $db;
		
		if (!defined('SYS_SEARCH')) require_once(BASE.'subsystems/search.php');
		
		$search = null;
		$search->category = 'Images';
		$search->ref_module = 'imagegallerymodule';
		$search->ref_type = 'imagegallery';
		
		if ($item) {
			$db->delete('search',"ref_module='imagegallerymodule' AND ref_type='imagegallery' AND original_id=" . $item->id);
			$search->original_id = $item->id;
				$search->title = ' ' . $item->name . ' '	;
				$search->view_link = 'index.php?action=view_gallery&module=imagegallerymodule&id='.$item->gallery_id;
				$search->body = ' ' . musicsmodule::make_small_part(exponent_search_removeHTML($item->alt)) . ' ';
				// get gallery
				$temp=$db->selectObject('imagegallery_gallery','id = ' . $item->gallery_id);
				$search->location_data = $temp->location_data;
			$db->insertObject($search,'search');
		} else {
			$db->delete('search',"ref_module='imagegallerymodule' AND ref_type='imagegallery'");
			foreach ($db->selectObjects('imagegallery_image') as $item) {
				$search->original_id = $item->id;
				$search->title = ' ' . $item->name . ' '	;
				$search->view_link = 'index.php?action=view_gallery&module=imagegallerymodule&id='.$item->gallery_id;
				$search->body = ' ' . musicsmodule::make_small_part(exponent_search_removeHTML($item->alt)) . ' ';
				// get gallery
				$temp=$db->selectObject('imagegallery_gallery','id = ' . $item->gallery_id);
				$search->location_data = $temp->location_data;
				$db->insertObject($search,'search');
			}
		}
		
		return true;
	}
}

?>
